package com.dmgis.qxfw.dataservice.businessservice.yydl.service.Impl;

import com.dmgis.qxfw.dataservice.businessservice.api.domain.DbConfig;
import com.dmgis.qxfw.dataservice.businessservice.api.domain.RequestDataBySelect;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.qx.important_weather;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.yj.warning;
import com.dmgis.qxfw.dataservice.businessservice.common.entity.SmallWindow;
import com.dmgis.qxfw.dataservice.businessservice.common.entity.Warning;
import com.dmgis.qxfw.dataservice.businessservice.common.util.JsonUtil;
import com.dmgis.qxfw.dataservice.businessservice.common.util.StringUtil;
import com.dmgis.qxfw.dataservice.businessservice.controller.ProcessRequestController;
import com.dmgis.qxfw.dataservice.businessservice.yydl.service.SmallWindowServiceI;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SmallWindowServiceImpl implements SmallWindowServiceI {

	ProcessRequestController pc = new ProcessRequestController();
	
	@Override
	public List<SmallWindow> getImpWtInfoTip(String date, String date1) {
		List<important_weather> listData = new ArrayList<important_weather>();
		List<SmallWindow> resultList = new ArrayList<SmallWindow>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add("stationid");
		columns.add("stationname");
		columns.add("to_char(observetime, 'yyyy-MM-dd HH24:MI:SS') as observetime");
		columns.add("weathertype");
		columns.add("weatherdetail");
		columns.add("lon");
		columns.add("lat");
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		int whereCount = 0;
		if (date != null) {
			strWhere.append(String.format("and observetime>='%s'", date));
		}
		if (date1 != null) {
			strWhere.append(String.format("and observetime<='%s'", date1));
		}
		if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere.toString());
		try {
			selectParam.put("method", "important_weather");
			selectParam.put("sqlType", "select");
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<important_weather>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		if (listData != null && listData.size() > 0) {
			for (important_weather weather : listData) {
				SmallWindow window = new SmallWindow();
				window.setStationId(weather.getStationid());
				window.setStationName(weather.getStationname());
				window.setObserveTime(weather.getObservetime());
				window.setForecastClass(weather.getWeathertype());
				window.setForecastInfo(weather.getWeatherdetail());
				window.setLon(weather.getLon().floatValue());
				window.setLat(weather.getLat().floatValue());
				resultList.add(window);
			}
		}
		return resultList;
	}
	@Override
	public List<Warning> warningInfoTip(String date, String date1) {
		List<warning> listData = new ArrayList<warning>();
		List<Warning> resultList = new ArrayList<Warning>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add("id");
		columns.add("area");
		columns.add("type");
		columns.add("color");
		columns.add("county");
		columns.add("standard");
		columns.add("defenseguide");
		columns.add("to_char(announcedtime, 'yyyy-MM-dd HH24:MI:SS') as announcedtime");
		columns.add("signatory");
		columns.add("watch");
		columns.add("pos");
		columns.add("bfree");
		columns.add("jcpath");
		columns.add("y1path");
		columns.add("y2path");
		columns.add("jb");
		columns.add("joinid");
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		selectRequest.setLimit(3);
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		int whereCount = 0;
		strWhere.append(String.format("and bfree='%s'", "0"));
		if (date != null) {
			strWhere.append(String.format("and announcedtime>='%s'", date));
		}
		if (date1 != null) {
			strWhere.append(String.format("and announcedtime<='%s'", date1));
		}
		if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere.toString());
		try {
			selectParam.put("method", "warn_signal_info");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<warning>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		if (listData != null && listData.size() > 0) {
			for (warning warn : listData) {
				Warning Warn = new Warning();
				Warn.setId(warn.getId());
				Warn.setArea(warn.getArea());
				Warn.setType(warn.getType());
				Warn.setColor(warn.getColor());
				Warn.setCounty(warn.getCounty());
				Warn.setStandard(warn.getStandard());
				Warn.setDefenseGuide(warn.getDefenseguide());
				Warn.setAnnouncedTime(warn.getAnnouncedtime());
				Warn.setSignatory(warn.getSignatory());
				Warn.setWatch(warn.getWatch());
				Warn.setPos(warn.getPos());
				Warn.setbFree(warn.getBfree());
				Warn.setJcpath(warn.getJcpath());
				Warn.setY1path(warn.getY1path());
				Warn.setY2path(warn.getY2path());
				Warn.setJb(warn.getJb());
				if(warn.getJoinid()!=null){
					Warn.setJoinID(warn.getJoinid());
				}else{
					Warn.setJoinID(-1);
				}
				resultList.add(Warn);
			}
		}
		return resultList;
	}

}
